﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public delegate void UserInputTouchBegin(Vector3 pos);
public delegate void UserInputTouchMoved(Vector3 pos);
public delegate void UserInputTouchEnded(Vector3 pos);
public class UserInput : MonoBehaviour
{
    public UserInputTouchBegin touchBegan;
    public UserInputTouchMoved touchMoved;
    public UserInputTouchEnded touchEnded;

    void Update()
    {
        Mouse();
        Touch();
    }

    // 处理鼠标的输入
    void Mouse()
    {
        if (Input.GetMouseButtonDown(0))
        {   // 按下
            touchBegan(Input.mousePosition);
        }
        else if (Input.GetMouseButtonUp(0))
        {
            touchEnded(Input.mousePosition);
        }

        if (Input.GetMouseButton(0))
        {
            touchMoved(Input.mousePosition);
        }

    }

    // 处理移动设备的输入
    void Touch()
    {
        if (Input.touchCount == 0)
        {
            return;
        }

        Touch touch = Input.GetTouch(0);
        if (touch.phase == TouchPhase.Began)
        {
            // 按下
            touchBegan(touch.position);
        }
        else if (touch.phase == TouchPhase.Moved)
        {
            // 滑动
            touchMoved(touch.position);
        }
        else if (touch.phase == TouchPhase.Ended)
        {
            // 抬起
            touchEnded(touch.position);
        }

    }
}
